package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureCostMaterialVo;
import org.dromara.agriculture.domain.bo.AgricultureCostMaterialBo;
import org.dromara.agriculture.service.IAgricultureCostMaterialService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 农资用量
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/costMaterial")
public class AgricultureCostMaterialController extends BaseController {

    private final IAgricultureCostMaterialService agricultureCostMaterialService;

    /**
     * 查询农资用量列表
     */
    @SaCheckPermission("agriculture:costMaterial:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureCostMaterialVo> list(AgricultureCostMaterialBo bo, PageQuery pageQuery) {
        return agricultureCostMaterialService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出农资用量列表
     */
    @SaCheckPermission("agriculture:costMaterial:export")
    @Log(title = "农资用量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureCostMaterialBo bo, HttpServletResponse response) {
        List<AgricultureCostMaterialVo> list = agricultureCostMaterialService.queryList(bo);
        ExcelUtil.exportExcel(list, "农资用量", AgricultureCostMaterialVo.class, response);
    }

    /**
     * 获取农资用量详细信息
     *
     * @param costId 主键
     */
    @SaCheckPermission("agriculture:costMaterial:query")
    @GetMapping("/{costId}")
    public R<AgricultureCostMaterialVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long costId) {
        return R.ok(agricultureCostMaterialService.queryById(costId));
    }

    /**
     * 新增农资用量
     */
    @SaCheckPermission("agriculture:costMaterial:add")
    @Log(title = "农资用量", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureCostMaterialBo bo) {
        return toAjax(agricultureCostMaterialService.insertByBo(bo));
    }

    /**
     * 修改农资用量
     */
    @SaCheckPermission("agriculture:costMaterial:edit")
    @Log(title = "农资用量", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureCostMaterialBo bo) {
        return toAjax(agricultureCostMaterialService.updateByBo(bo));
    }

    /**
     * 删除农资用量
     *
     * @param costIds 主键串
     */
    @SaCheckPermission("agriculture:costMaterial:remove")
    @Log(title = "农资用量", businessType = BusinessType.DELETE)
    @DeleteMapping("/{costIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] costIds) {
        return toAjax(agricultureCostMaterialService.deleteWithValidByIds(List.of(costIds), true));
    }
}
